﻿Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Imports System.Web.Script.Services
Imports System.Web.Services.Protocols
Imports VSModel
Imports System.Data.Objects
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Converters
Imports Cls_Common
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class swCheckData
    Inherits System.Web.Services.WebService

    <WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function CheckName(ByVal strName As String) As String
        Using _data As New VSEntities
            If strName.Trim <> "" Then
                Dim p = (From q In _data.Users Where q.UserName = strName Select q).FirstOrDefault
                If Not p Is Nothing Then
                    Return "0"
                Else
                    Return "1"
                End If
            Else
                Return "1"
            End If
        End Using
    End Function
    <WebMethod()> _
     <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function CheckCardId(ByVal strCarId As String, ByVal intHuyen As Integer) As String
        Using _data As New VSEntities
            'cmnd kiem tra toan tinh
            If strCarId.Trim <> "" Then
                Dim p = (From q In _data.CertifiedHouseholds _
                                         Where q.Ow_CardID = strCarId Select q).FirstOrDefault
                If Not p Is Nothing Then
                    Dim lst = (From k In _data.CertifiedChamDuts Join h In _data.CertifiedHouseholds On k.CertifiedCode Equals h.CertifiedCode Where h.Ow_CardID = strCarId).ToList
                    If lst.Count > 0 Then
                        Return "1"
                    End If
                    Return "0"
                Else
                    'check them ko co' trong cham dut

                    Return "1"
                End If
            Else
                Return "1"
            End If
        End Using
    End Function
    <WebMethod()> _
        <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function CheckTenHo(ByVal strTenHo As String, ByVal intHuyen As Integer) As String
        Using _data As New VSEntities
            If strTenHo.Trim <> "" Then
                Dim p = (From q In _data.CertifiedHouseholds _
                         Where q.Biz_VietName = strTenHo And q.ProvinceId = intHuyen Select q).FirstOrDefault
                If Not p Is Nothing Then
                    Return "0"
                Else
                    Return "1"
                End If
            Else
                Return "1"
            End If
        End Using
    End Function

    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function CheckTenHoCapDoi(ByVal strTenHo As String, ByVal intHuyen As Integer, ByVal intCertifiedId As Integer) As String
        Using _data As New VSEntities
            If strTenHo.Trim <> "" Then

                Dim p = (From q In _data.CertifiedHouseholds _
                         Where q.Biz_VietName = strTenHo And q.ProvinceId = intHuyen And q.CertifiedID <> intCertifiedId Select q).FirstOrDefault
                If Not p Is Nothing Then
                    Return "0"
                Else
                    Return "1"
                End If
            Else
                Return "1"
            End If
        End Using
    End Function
    <WebMethod()> _
     <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function CheckCardIdCapDoi(ByVal strCarId As String, ByVal intHuyen As Integer, ByVal intCertifiedId As Integer) As String
        Using _data As New VSEntities
            'cmnd kiem tra toan tinh
            If strCarId.Trim <> "" Then
                Dim p = (From q In _data.CertifiedHouseholds _
                         Where q.Ow_CardID = strCarId And q.CertifiedID <> intCertifiedId Select q).FirstOrDefault
                If Not p Is Nothing Then
                    Return "0"
                Else
                    Return "1"
                End If
            Else
                Return "1"
            End If
        End Using
    End Function
    <WebMethod()> _
 <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function GetDongXe(ByVal strParent As String) As String
        Using _data As New VSEntities
            If strParent.Trim <> "" Then
                Dim intParent As Integer = CInt(strParent)
                Dim p = (From q In _data.Categories Where q.ParentId = intParent Select q.CatId, q.Title).FirstOrDefault
                Dim serializer As New JsonSerializer
                serializer.Converters.Add(New JavaScriptDateTimeConverter())
                serializer.NullValueHandling = NullValueHandling.Ignore
                Dim sJSON As String = JsonConvert.SerializeObject(p)
                Return sJSON
            Else
                Return "1"
            End If
        End Using
    End Function

    <WebMethod(EnableSession:=True)> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function IncreaseLogin() As String
        Context.Session("LastLogin") = Now
        Dim _UserId As Integer = Session("UserId")
        Context.Session("UserId") = _UserId
        Dim _HuyenId = Context.Session("HuyenId")
        Context.Session("HuyenId") = _HuyenId
        Return "1"
    End Function

End Class